﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Lime;

namespace Clandestine.Graphics.Window
{
    // This is GameWindow, without a timing loop.
    // (The whole point of the IWindowHandler infrastructure is to level the ground between GlControl and GameWindow.)
    public class GraphicsWindowHandler : WindowHandler, IDisposable
    {
        private GraphicsWindow window;
        private GraphicsContext context;

        // Note re. #pragma warning disabling, and the use of INativeWindow.InputDriver:
        //  We use the deprecated input stuff, because it's the only thing that works right now.
        //  When OpenTK get around to revising their input stuff, it is essential we re-do all input stuff, properly.

        public GraphicsWindowHandler(GraphicsWindow window)
        {
            this.window = window;

            // OpenGL context, yeah!
            createContext();
        }

        private void createContext()
        {
			
            context = new GraphicsContext(GraphicsMode.Default, window.NativeWindow.WindowInfo);
			context.SetupAuto(window.NativeWindow);
            window.NativeWindow.Visible = true;
        }
        
        public override void MakeContextCurrent()
        {
            context.MakeCurrent(window.NativeWindow.WindowInfo);
        }

        public override void ReleaseContext()
        {
            context.MakeCurrent(null);
        }

        public override void ProcessEvents() { }  // GraphicsWindow does this in it's own thread.

        public override void SwapBuffers()
        {
            context.SwapBuffers();
        }

        #pragma warning disable 0612
        public override OpenTK.Input.KeyboardDevice Keyboard
        {
            get { return window.NativeWindow.InputDriver.Keyboard[0]; }
        }
        #pragma warning restore 0612

        #pragma warning disable 0612
        public override OpenTK.Input.MouseDevice Mouse
        {
            get { return window.NativeWindow.InputDriver.Mouse[0]; }
        }
        #pragma warning restore 0612

        public void Dispose()
        {
            window.Stop();
            context.Dispose();
        }
    }
}
